#include "mainwindow.h"
#include "ui_mainwindow.h"


#include <QPainter>
#include <QMouseEvent>
#include <QDebug>
#include <QMessageBox>
#include <QTimer>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //设置窗口大小
    this->setFixedSize(MARGIN * 2 + BLOCK_SIZE * (BOARD_GRAD_SIZE - 1),MARGIN * 2 + BLOCK_SIZE * (BOARD_GRAD_SIZE - 1));
    this->setMouseTracking(true);//鼠标跟随
    this->centralWidget()->setMouseTracking(true);//两个都要加
    m_posRow = 0;
    m_posCol = 0;
    m_needPoint = false;
    initGames();

    startNewGame = false;

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    painter.setRenderHint(QPainter::Antialiasing,true);//抗锯齿

    for(int i = 0; i < BOARD_GRAD_SIZE;i++){
        //水平
        painter.drawLine(MARGIN,MARGIN+BLOCK_SIZE*i,MARGIN+BLOCK_SIZE*(BOARD_GRAD_SIZE - 1),MARGIN+BLOCK_SIZE*i);
        //垂直
        painter.drawLine(MARGIN+BLOCK_SIZE*i,MARGIN,MARGIN+BLOCK_SIZE*i,MARGIN+BLOCK_SIZE*(BOARD_GRAD_SIZE - 1));
    }

    QBrush brush;
    brush.setStyle(Qt::SolidPattern);

    //画棋子
    for(int i = 0; i < BOARD_GRAD_SIZE; i++){
        for(int j = 0; j < BOARD_GRAD_SIZE; j++){
            if(game->gameMapVec[i][j] == 1){
                brush.setColor(Qt::black);
                painter.setBrush(brush);
                painter.drawEllipse(MARGIN + BLOCK_SIZE * j - CHESS_RADIUS,
                                    MARGIN + BLOCK_SIZE * i - CHESS_RADIUS,
                                    CHESS_RADIUS * 2,
                                    CHESS_RADIUS * 2);
            }
            else if(game->gameMapVec[i][j] == -1){
                brush.setColor(Qt::white);
                painter.setBrush(brush);
                painter.drawEllipse(MARGIN + BLOCK_SIZE * j - CHESS_RADIUS,
                                    MARGIN + BLOCK_SIZE * i - CHESS_RADIUS,
                                    CHESS_RADIUS * 2,
                                    CHESS_RADIUS * 2);
            }
        }
    }

    //画点
    if(!m_needPoint) return;
    //绘制落子标记
    if(game->gameMapVec[m_posRow][m_posCol] != 0){
        return;
    }
    if(game->playerFlag)
        brush.setColor(Qt::blue);
    else
        brush.setColor(Qt::white);
    painter.setBrush(brush);
    //painter.drawPoint(m_drawPosx,m_drawPosy);
    painter.drawRect(m_drawPosx-2,m_drawPosy-2,4,4);

}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    m_needPoint = false;
    int pointx = event->x();
    int pointy = event->y();

    //15 * 15包括边缘就是考虑是 17* 17不包括边缘：最左边  MARGIN - BLOCK_SIZE 最右边 MARGIN + BLOCK_SIZE * (BOARD_GRAD_SIZE+1)
    if(pointx < MARGIN - BLOCK_SIZE
            || pointx > MARGIN + BLOCK_SIZE * (BOARD_GRAD_SIZE+1)
            || pointy < MARGIN - BLOCK_SIZE
            || pointy > MARGIN + BLOCK_SIZE * (BOARD_GRAD_SIZE+1)){
        update();
        return;
    }
    //鼠标所在的左上角的点 行 列
    int leftUpHang = (pointy - MARGIN + BLOCK_SIZE) / BLOCK_SIZE;
    int leftUpLie = (pointx - MARGIN + BLOCK_SIZE) / BLOCK_SIZE;

    //qDebug()<<"ooo "<<leftUpHang << leftUpLie;
    //判断离哪个点最近 x距离小于格子大小的1/3 y的距离也是
    //最近的点的坐标
    int showPointx = 0,showPointy = 0;
    if((pointx - MARGIN + BLOCK_SIZE - leftUpLie*BLOCK_SIZE) < POS_OFFSET ){
        //左上或左下
        showPointx = MARGIN - BLOCK_SIZE + leftUpLie *BLOCK_SIZE;
        m_posCol = leftUpLie - 1;
      //  qDebug()<<"1"<<m_posCol;
    }else if((MARGIN - BLOCK_SIZE + (leftUpLie + 1)*BLOCK_SIZE - pointx) < POS_OFFSET ){
        //右上或右下
        showPointx = MARGIN - BLOCK_SIZE + (leftUpLie + 1)*BLOCK_SIZE;
        m_posCol = leftUpLie;
       // qDebug()<<"2"<<m_posCol;
    }
    if((pointy - MARGIN + BLOCK_SIZE - leftUpHang*BLOCK_SIZE) < POS_OFFSET){
        //上面的点
        showPointy = MARGIN - BLOCK_SIZE + leftUpHang * BLOCK_SIZE;
        m_posRow = leftUpHang - 1;
       // qDebug()<<"3"<<m_posRow;
    }
    else if((MARGIN - BLOCK_SIZE + (leftUpHang + 1)*BLOCK_SIZE - pointy) < POS_OFFSET ){
        //下面的点
        showPointy = MARGIN - BLOCK_SIZE + (leftUpHang + 1)*BLOCK_SIZE;
        m_posRow = leftUpHang;
        //qDebug()<<"4"<<m_posRow;
    }
    if(!(m_posRow >= 0 && m_posCol >= 0 && m_posRow < BOARD_GRAD_SIZE && m_posCol < BOARD_GRAD_SIZE)){
        update();
        return;
    }
    if(showPointx > 0 && showPointy >0){
        //画点点
        //x坐标
        m_drawPosx = showPointx;
        m_drawPosy = showPointy;
//        m_posRow = leftUpHang - 1;
//        m_posCol = leftUpLie - 1;
        m_needPoint = true;
        //qDebug()<<"yes!!!"<<m_drawPosx << m_drawPosy;

    }

    update();
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if(!m_needPoint) return;
    m_needPoint = false;

    //人落子
    qDebug()<<"lalala";
    startNewGame = false;
    chessOneByPerson();


    if(game_type == AI){
        //AI下棋
        if(startNewGame) return;
        QTimer::singleShot(AI_THINK_TIME,this,SLOT(chessOneByAI()));
    }
}

void MainWindow::chessOneByAI()
{

    //如果重新开始了 就不下了 return
    //单步
    game->actionByAI(m_posRow,m_posCol);
    //多步
    //game->actionByAI2(m_posRow,m_posCol);
    //声音
    //QSound::play("");
    qDebug()<<"kada!";
    //重绘
    update();
    //判断
    haveWinner(m_posRow,m_posCol);
}

void MainWindow::initGames()
{
    game = new GameModel;
    initAIGames();
}

void MainWindow::initAIGames()
{
    game_type = AI;
    game->gameStatus = PLAYING;
    game->startGame(game_type);
    update();
}

void MainWindow::chessOneByPerson()
{
    //根据当前存储的坐标下子
    //只有有效点击才下子，并且该处没有子
    if(game->gameMapVec[m_posRow][m_posCol] == 0){
        game->actionByPerson(m_posRow,m_posCol);
        //播放落子音效，待实现
        qDebug()<<"kada!";
        //重绘
        update();
        startNewGame = false;

        //判断
        haveWinner(m_posRow,m_posCol);
    }
    else{
        startNewGame = true;
    }
}

void MainWindow::haveWinner(int row, int col)
{

    if(game->isWin(row,col) && game->gameStatus == PLAYING){
        QString winner;
        if(!game->playerFlag){
            //人胜利了
            winner = "玩家";
        }else{
            //电脑胜利了
            winner = "电脑";
        }
        //重置游戏装填 否则容易死循环
        QMessageBox msgBox;
        msgBox.setText("本轮结束了.");
        msgBox.setInformativeText(winner+"胜利！");
        msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
        msgBox.setDefaultButton(QMessageBox::Ok);
        int ret = msgBox.exec();
        if(ret == QMessageBox::Ok){
            game->startGame(game_type);
            game->gameStatus = PLAYING;
            startNewGame = true;
        }
    }
}

